﻿using Microsoft.AspNetCore.Mvc;
using SHRIS.MIS.Business.SystemManage;
using SHRIS.MIS.Common.Model;
using SHRIS.MIS.Common.Util;
using SHRIS.MIS.Common.Util.Model;
using SHRIS.MIS.Entity.LonginState;
using SHRIS.MIS.Entity.SystemManage;
using SHRIS.MIS.Model.Param.SystemManage;
using SHRIS.MIS.Model.Result;
using SHRIS.MIS.Model.Result.SystemManage;
using SHRIS.MIS.Web.Controllers;
using SHRIS.MIS.Web.Filter;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SHRIS.MIS.Web.Areas.SystemManage.Controllers
{
    [Area("SystemManage")]
    public class UserController : BaseController
    {
        #region 服务注入
        private UserBLL _userBLL;
        public UserController(UserBLL userBLL)
        {
            _userBLL = userBLL;
        }
        #endregion

        #region 视图功能
        [AuthorizeFilter("system:user:view")]
        public IActionResult UserIndex()
        {
            return View();
        }

        public IActionResult UserForm()
        {
            return View();
        }

        public IActionResult UserDetail()
        {
            ViewBag.Ip = NetHelper.Ip;
            return View();
        }

        public IActionResult ResetPassword()
        {
            return View();
        }

        public async Task<IActionResult> ChangePassword()
        {
            ViewBag.OperatorInfo = await Operator.Instance.Current();
            return View();
        }

        public IActionResult ChangeUser()
        {
            return View();
        }

        public async Task<IActionResult> UserPortrait()
        {
            ViewBag.OperatorInfo = await Operator.Instance.Current();
            return View();
        }

        public IActionResult UserImport()
        {
            return View();
        }
        #endregion

        #region 获取数据
        [HttpGet]
        [AuthorizeFilter("system:user:search")]
        public async Task<IActionResult> GetListJson(UserListParam param)
        {
            TEntity<List<UserEntity>> obj = await _userBLL.GetList(param);
            return Json(obj);
        }

        [HttpGet]
        [AuthorizeFilter("system:user:search")]
        public async Task<IActionResult> GetPageListJson(UserListParam param, Pagination pagination)
        {
            TEntity<List<UserEntity>> obj = await _userBLL.GetPageList(param, pagination);
            return Json(obj);
        }

        [HttpGet]
        public async Task<IActionResult> GetFormJson(Guid id)
        {
            TEntity<UserEntity> obj = await _userBLL.GetEntity(id);
            return Json(obj);
        }

        [HttpGet]
        public async Task<IActionResult> GetUserAuthorizeJson([FromServices]MenuAuthorizeBLL menuAuthorizeBLL)// 方法注入
        {
            TEntity<UserAuthorizeInfo> obj = new TEntity<UserAuthorizeInfo>();
            OperatorInfo operatorInfo = await Operator.Instance.Current();
            TEntity<List<MenuAuthorizeInfo>> objMenuAuthorizeInfo = await menuAuthorizeBLL.GetAuthorizeList(operatorInfo);
            obj.Data = new UserAuthorizeInfo();
            obj.Data.IsSystem = operatorInfo.IsSystem;
            if (objMenuAuthorizeInfo.Tag == 1)
            {
                obj.Data.MenuAuthorize = objMenuAuthorizeInfo.Data;
            }
            obj.Tag = 1;
            return Json(obj);
        }
        #endregion

        #region 提交数据
        [HttpPost]
        [AuthorizeFilter("system:user:add,system:user:edit")]
        public async Task<IActionResult> SaveFormJson(UserEntity entity)
        {
            TEntity<string> obj = await _userBLL.SaveForm(entity);
            return Json(obj);
        }

        [HttpPost]
        [AuthorizeFilter("system:user:delete")]
        public async Task<IActionResult> DeleteFormJson(string ids)
        {
            TEntity obj = await _userBLL.DeleteForm(ids);
            return Json(obj);
        }

        [HttpPost]
        [AuthorizeFilter("system:user:resetpassword")]
        public async Task<IActionResult> ResetPasswordJson(UserEntity entity)
        {
            TEntity<Guid> obj = await _userBLL.ResetPassword(entity);
            return Json(obj);
        }

        [HttpPost]
        public async Task<IActionResult> ChangePasswordJson(ChangePasswordParam entity)
        {
            TEntity<Guid> obj = await _userBLL.ChangePassword(entity);
            return Json(obj);
        }

        [HttpPost]
        public async Task<IActionResult> ChangeUserJson(UserEntity entity)
        {
            TEntity<Guid> obj = await _userBLL.ChangeUser(entity);
            return Json(obj);
        }
        #endregion
    }
}